<template>
  <div class="orders">
    <!-- 面包屑组件 -->
    <Breadcrumb name1="订单管理" name2="订单查询"></Breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="7">
          <!-- 搜索与添加区域 -->
          <el-input
            placeholder="请输入email名称"
            v-model="email"
            clearable
            @clear="getOrderList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderByEmail"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表 -->
      <el-table :data="orderList" stripe style="width: 100%" border>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column prop="phone" label="手机号"></el-table-column>
        <el-table-column prop="address" label="地址"></el-table-column>
        <el-table-column label="创建订单时间" width="180px">
          <template slot-scope="scope">
            <span>{{ scope.row.datetime | formatDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="商品信息" width="180px">
          <template v-slot="scope">
            <el-popover placement="right" width="400" trigger="click">
              <el-table :data="goods">
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column
                  property="brand"
                  label="品牌"
                ></el-table-column>
                <el-table-column
                  width="200px"
                  property="name"
                  label="名称"
                ></el-table-column>
                <el-table-column
                  property="price"
                  label="价格(元)"
                ></el-table-column>
                <el-table-column
                  property="count"
                  label="数量"
                ></el-table-column>
              </el-table>
              <el-button
                slot="reference"
                type="info"
                @click="showGoods(scope.row)"
                >点击查看商品详情</el-button
              >
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="orderId" label="订单号"></el-table-column>
        <el-table-column label="发货状态" width="80px">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 2" :style="{ color: '#E6A23C' }">
              未发货
            </span>
            <span v-else :style="{ color: '#67C23A' }"> 已发货</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <!-- 修改按钮 -->
            <el-popover placement="top" width="200">
              <div style="text-align: center; margin: 0">
                <el-button
                  size="mini"
                  type="warning"
                  @click="changeStatus(scope.row, 2)"
                >
                  未发货</el-button
                >
                <el-button
                  type="success"
                  size="mini"
                  @click="changeStatus(scope.row, 1)"
                  >已发货</el-button
                >
              </div>
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="small"
                slot="reference"
              ></el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <Paging :paging="paging" @change="changepage"></Paging>
    </el-card>
  </div>
</template>
<script>
import Breadcrumb from '@/components/breadcrumb'
import Paging from '@/components/paging'
import { Message } from 'element-ui'
import { getOrderList, getOrderByEmail, updateOrderStatus } from '@/api'
export default {
  data() {
    return {
      orderList: [],
      paging: {
        page: 1,
        limit: 5,
        total: 0,
      },
      email: '',
      goods: [],
    }
  },
  components: {
    Breadcrumb,
    Paging,
  },
  created() {
    this.getOrderList()
  },
  methods: {
    async getOrderList() {
      const { data: res } = await getOrderList({
        page: this.paging.page,
        limit: this.paging.limit,
      })
      if (res.code === 1) {
        this.orderList = res.data
        this.paging.total = res.total[0].total
        Message.success(res.msg)
      } else {
        Message.error(res.msg)
      }
    },
    async getOrderByEmail() {
      console.log(this.paging, this.email)
      const { data: res } = await getOrderByEmail({
        page: this.paging.page,
        limit: this.paging.limit * 2,
        email: this.email.trim(),
      })
      console.log(res)
      if (res.code === 1) {
        this.orderList = res.data
        this.paging.total = res.total
        Message.success(res.msg)
      } else {
        Message.error(res.msg)
      }
    },
    changepage(page) {
      this.paging.page = page
      this.getOrderList()
    },
    showGoods(row) {
      this.goods = JSON.parse(row.goods)
    },
    changeStatus(row, status) {
      const { orderId } = row
      console.log(orderId, status)
      this.$confirm('确定更改发货状态吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          const { data: res } = await updateOrderStatus({
            orderId,
            status,
          })
          if (res.code === 1) {
            Message.success(res.msg)
            this.getOrderList()
          } else {
            Message.error(res.msg)
          }
        })
        .catch(() => {
          Message.info('取消更改')
        })
    },
  },
}
</script>
<style lang="scss" scoped>
.el-table {
  margin-top: 15px;
  margin-bottom: 10px;
}
</style>
